package com.tokenplus.thai.newcomerenjoy

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.tokenplus.thai.R
import com.tokenplus.thai.aviewmodel.CoinsViewModel
import com.tokenplus.thai.aviewmodel.ConfigViewModel
import com.tokenplus.thai.aviewmodel.NewComerEnjoyViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.components.dialog.selectAsset.SelectAssetDialog
import com.tokenplus.thai.data.ReqBase
import com.tokenplus.thai.databinding.ActivityNewComerEnjoyBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.startToTransferActivity
import com.tokenplus.thai.extend.toFinancePercentString
import com.tokenplus.thai.extend.toFinanceString
import com.tokenplus.thai.home.repo.HomeCoinManager

/** coupons页 */
@SuppressLint("CustomSplashScreen")
class NewComerEnjoyActivity : BaseActivity() {

    //布局binding
    private lateinit var mBinding: ActivityNewComerEnjoyBinding

    //select asset 弹框
    private lateinit var mBottomSheetDialog: SelectAssetDialog

    //home viewModel
    private val mNewComerEnjoyViewModel by lazy {
        ViewModelProvider(this)[NewComerEnjoyViewModel::class.java]
    }

    //config viewModel
    private val mConfigViewModel by lazy {
        ViewModelProvider(this)[ConfigViewModel::class.java]
    }

    //coins viewModel
    private val mCoinsViewModel by lazy {
        ViewModelProvider(this)[CoinsViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mNewComerEnjoyViewModel)
        list.add(mCoinsViewModel)
        list.add(mConfigViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.coupons.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mBinding =
            ActivityNewComerEnjoyBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化刷新组件
            initRefreshUI()

            //初始化弹框
            initSelectAssetDialog()

            //监听返回
            onHandleLiveDataCallback()

            // 点击转入
            mBinding.transferIn.setOnClickListener {
                mCoinsViewModel.getCoins(ReqBase(showLoading = false))
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        //获取返现信息
        getNewComerEnjoy()

        // 获取首充配置
        getFirstRechargeConfig()
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        mNewComerEnjoyViewModel.onGetNewComerEnjoyLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mBinding.callbackAmount.text = result.income.toFinanceString()
                    mBinding.transferAmount.text = result.amount.toFinanceString()

                    if (result.first == "NO") {
                        mBinding.transferIn.setBackgroundResource(R.drawable.callback_transfer_in)
                        mBinding.transferIn.setTextColor(Color.parseColor("#788ACD"))
                        mBinding.transferIn.isEnabled = true
                    } else {
                        mBinding.transferIn.setBackgroundResource(R.drawable.callback_transfer_in_unable)
                        mBinding.transferIn.setTextColor(Color.parseColor("#B3B3B3"))
                        mBinding.transferIn.isEnabled = false
                    }
                }
            }
        }

        mConfigViewModel.onGetFirstRechargeConfigLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mBinding.enjoyDetail.text =
                        getString(
                            R.string.new_comer_welfare_banner2,
                            result.toFinancePercentString()
                        )

                    mBinding.callbackTipsDetail.text =
                        getString(
                            R.string.new_comer_welfare_tips_detail,
                            result.toFinancePercentString()
                        )

                    mBinding.callbackTransfertTips.text =
                        getString(
                            R.string.new_comer_welfare_transfer1,
                            result.toFinancePercentString()
                        )
                }
            }
        }

        mCoinsViewModel.onGetCoinsLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    HomeCoinManager.mCoins.clear()
                    HomeCoinManager.mCoins.addAll(result)

                    mBottomSheetDialog.updateCurrentTitle(HomeCoinManager.mGlobalNameLiveData.value)
                    mBottomSheetDialog.updateDataList(HomeCoinManager.mCoins)
                    mBottomSheetDialog.show()
                }
            }
        }
    }

    /** 获取返现信息 */
    private fun getNewComerEnjoy() {
        val reqBase = ReqBase()
        reqBase.showLoading = true
        mNewComerEnjoyViewModel.getNewComerEnjoy(reqBase)
    }

    /** 获取首充配置信息 */
    private fun getFirstRechargeConfig() {
        val reqBase = ReqBase()
        reqBase.showLoading = false
        mConfigViewModel.getFirstRechargeConfig(reqBase)
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mBinding.swipeRefresh) {
            getNewComerEnjoy()
            getFirstRechargeConfig()
        }
    }

    /** 初始化 select asset 弹框 */
    @SuppressLint("NotifyDataSetChanged")
    private fun initSelectAssetDialog() {
        mBottomSheetDialog = SelectAssetDialog(this)
        mBottomSheetDialog.setItemClickListener { revDataAsset ->
            //组装参数
            val coinType = revDataAsset.coinType ?: ""
            val params = HashMap<String, String>()
            params["coinType"] = coinType
            startToTransferActivity(params)
        }
        mBottomSheetDialog.setItemInputListener { }
    }
}